from django.db import models
from decimal import Decimal

# Create your models here.
"""
这个app用来处理混合表结构的视图虚表
"""


class ViewGoodsBlockWithProductInfo(models.Model):
    """
    统计当前查询时刻用户持仓的所有区块的关联产品的最新价格
    create or replace view view_goodsblock_with_product_info as
        select
            gb.id,
            p.last_trade_price,
            gb.owner_user_id,
            p.avg_price_custom as avg_price
        from
            product_goodsblock as gb ,
            product_product as p
        where
            gb.product_id = p.id
            and gb.owner_user_id is not null
            and gb.owner_user_id != '';
    """

    class Meta:
        managed = False
        db_table = 'view_goodsblock_with_product_info'

    id = models.CharField(verbose_name='ID', max_length=64, primary_key=True)
    last_trade_price = models.DecimalField(max_digits=10, decimal_places=2, default=Decimal('0.00'))
    owner_user_id = models.CharField("拥有者用户ID", blank=True, null=True, max_length=64)
    avg_price = models.DecimalField("历史平均价", max_digits=10, decimal_places=2)

class ViewUserProduct(models.Model):
    """
    用户持仓视图

    create or replace view view_user_product as
    select
        a.owner_user_id,
        a.product_id,
        a.user_keep_count,
        p.logo_public_file_id,
        p.name,
        p.last_trade_price,
        p.up_down_percent,
        p.avg_price_custom as avg_price,
        a.owner_user_id || a.product_id as id,
        p.brand_id,
        b.name as brand_name
    from
        (select count(*) as user_keep_count, owner_user_id, product_id from product_goodsblock where owner_user_id != '' and trade_type != 'TAKE_OUT' group by owner_user_id, product_id) as a,
        product_product as p,
        product_brand as b
    where
        a.product_id=p.id and p.brand_id = b.id;
    """

    class Meta:
        managed = False
        db_table = 'view_user_product'

    id = models.CharField(verbose_name='OwnerUserID+ProductId', max_length=64, primary_key=True)
    owner_user_id = models.CharField(verbose_name='OwnerUserID', blank=True, null=True, max_length=64)
    product_id = models.CharField(verbose_name='ProductID', blank=True, null=True, max_length=64)
    user_keep_count = models.IntegerField(verbose_name='用户持有该产品的量')
    logo_public_file_id = models.CharField("ProductLogo公开文件ID", max_length=64)
    name = models.CharField("产品名", max_length=256)
    last_trade_price = models.DecimalField("最后一次成交价", max_digits=10, decimal_places=2)
    up_down_percent = models.DecimalField("涨跌幅", max_digits=5, decimal_places=2)
    avg_price = models.DecimalField("历史平均价", max_digits=10, decimal_places=2)  # 依据GoodsBlock里的avg_price实时更新
    brand_id = models.CharField(verbose_name='BrandID', blank=True, null=True, max_length=64)
    brand_name = models.CharField("BrandName", max_length=256)

    def as_dict(self):
        from apps.product.models import Product
        return {
            "id": self.product_id,
            "user_keep_count": self.user_keep_count,
            "name": self.name,
            "last_trade_price": self.last_trade_price,
            "up_down_percent": self.up_down_percent * 100,
            "avg_price": self.avg_price,
            "logo": self.logo,
            "brand_name": self.brand_name,
            'is_comment': Product.objects.get(pk=self.product_id).commented_by(self.owner_user_id),
        }

    @property
    def logo_public_file(self):
        from apps.storage.models import PublicFile
        return PublicFile.objects.filter(pk=self.logo_public_file_id).first()

    @property
    def logo(self):
        return self.logo_public_file.url if self.logo_public_file else ''
